﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Text.RegularExpressions;

namespace IService.DataDLL.Utility
{
    public class StringUtil
    {
        /// <summary>
        /// 将内容去除html标记后，截取相应长度前n个字符
        /// </summary>
        /// <param name="content"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetShortContent(string content, int length)
        {
            string str = HtmlDecode.HtmlDeleteCode(content);
            return GetFirstString(str, length);
        }
        /// <summary>
        /// 获取前n个字符
        /// </summary>
        /// <param name="stringToSub"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetFirstString(string stringToSub, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder sb = new StringBuilder();
            int nLength = 0;
            bool isCut = false;
            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    sb.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    sb.Append(stringChar[i]);
                    nLength = nLength + 1;
                }
                if (nLength > length)
                {
                    isCut = true;
                    break;
                }
            }
            if (isCut)
                return sb.ToString();//可以带上结尾
            else
                return sb.ToString();
        }
        /// <summary>
        /// 将日期字符串转化为某种格式字符串
        /// </summary>
        /// <param name="DateString"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string ConvertDateString(string DateString, string format)
        {
            try
            {
                DateTime time = Convert.ToDateTime(DateString);
                return time.ToString(format);
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }

        public static string GetAppConfigString(string appstring)
        {
            string AppString = System.Configuration.ConfigurationManager.AppSettings[appstring];
            if (!string.IsNullOrEmpty(AppString))
            {
                return AppString;
            }
            return string.Empty;
        }

        public static int GetAppConfigInt(string appstring)
        {
            int ReturnInt = int.MinValue;
            string AppString = System.Configuration.ConfigurationManager.AppSettings[appstring];
            if (!string.IsNullOrEmpty(AppString))
            {
                Regex regex = new Regex("^[0-9]*[1-9][0-9]*$");
                if (regex.IsMatch(AppString))
                {
                    ReturnInt = int.Parse(AppString);
                }
            }
            return ReturnInt;
        }
        /// <summary>
        /// 获取当前年份
        /// </summary>
        public static int GetYearFromCurrent
        {
            get
            {
                return DateTime.Now.Year;
            }
        }

        /// <summary>
        /// 获取当前月份
        /// </summary>
        public static int GetMonthFromCurrent
        {
            get
            {
                return DateTime.Now.Month;
            }
        }
    }
}
